package com.dongdongshop.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.bo.CartBO;
import com.dongdongshop.constant.CartConstant;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.util.CookieUtils;
import com.dongdongshop.util.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @DubboReference(timeout = 1000000)
    private CartService cartService;


    //未登录:从cookie中存取
    //已登录:redis中存取
    @RequestMapping("getCart")
    @ResponseBody
    public List<CartBO> getCart(HttpServletRequest request,HttpServletResponse response) {
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        String cookieValue = CookieUtils.getCookieValue(request, CartConstant.CART_LIST, true);
        if (cookieValue == null || "".equals(cookieValue)) {//防止空指针
            cookieValue = "[]";
        }
        List<CartBO> cartBOS = JSONObject.parseArray(cookieValue, CartBO.class);
        if (user == null) {
            return cartBOS;

        } else {
            //1.已登录,从redis中取出购物车数据
            List<CartBO> cartList = cartService.getCartFromRedis(user.getId());
            //先判断cookie中是否有值,乳沟有值我们就合并,没有值,说明合并过了,就直接返回redis中的购物车数据
            if (cartBOS.isEmpty()){
                return cartList;
            }
            //2.将cookie中的购物车数据与redis中数据进行合并
            List<CartBO> mergeList = cartService.merge(cartBOS, cartList);
            //3.合并成功之后,还要将合并后的购物车数据继续存放在redis中
            cartService.addCartToRedis(user.getId(), mergeList);
            //4.删除cookie中的数据
            CookieUtils.deleteCookie(request, response, CartConstant.CART_LIST);
            //5.返回合并后的数据
            return mergeList;
        }


    }

    @RequestMapping("addCart")
    @ResponseBody
    public Result addCart(Long itemId, Integer num, HttpServletRequest request, HttpServletResponse response) {
        try {
            //往购物车列表中添加新的商品
            List<CartBO> cartList = getCart(request,response);
            List<CartBO> cartBOS = cartService.addCart(cartList, itemId, num);
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            if (user == null) {
                //写入到cookie中
                String s = JSONObject.toJSONString(cartBOS);
                CookieUtils.setCookie(request, response, CartConstant.CART_LIST, s, true);
            } else {
                cartService.addCartToRedis(user.getId(), cartBOS);
            }

            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("addCarts")
    public String addCarts(Long itemId, Integer num, HttpServletRequest request, HttpServletResponse response) {
        try {
            TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();
            //获取购物车
            List<CartBO> cartBOList = getCart(request,response);
            //调用购物车服务
            cartBOList = cartService.addCart(cartBOList, itemId, num);
            if (tbUser == null){//未登录添加到cookie
                String s = JSONObject.toJSONString(cartBOList);
                CookieUtils.setCookie(request, response, CartConstant.CART_LIST, s, true);
            }else {//登录存redis
                cartService.addCartToRedis(tbUser.getId(),cartBOList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success-cart";
    }

    @RequestMapping("show")
    public String show() {
        return "cart";
    }

    @RequestMapping("deleteCartList")
    @ResponseBody
    private String deleteCartList(HttpServletRequest request, HttpServletResponse response) {
        CookieUtils.deleteCookie(request, response, CartConstant.CART_LIST);
        return "delete ok";
    }


    @RequestMapping("ajaxGetCart")
    @ResponseBody
    public Result ajaxGetCart(HttpServletRequest request, HttpServletResponse response) {
        List<CartBO> cart = getCart(request, response);
        return Result.OK().setData(cart);
    }
}
